package com.hengxinguotong.zhihuichengjian.ui.device;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceListPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.bean.Device;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设备拆卸
 * Created by liux on 2017/3/13.
 */

public class DeviceDisassembleActivity extends BaseActivity{
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.save_tv)
    HXTextView saveTv;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.in_rb)
    RadioButton inRb;
    @BindView(R.id.out_rb)
    RadioButton outRb;
    @BindView(R.id.in_out_top)
    RadioGroup inOutTop;
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.device_viewpager)
    ViewPager deviceViewpager;
    private DeviceListPagerAdapter pagerAdapter;
    private DeviceDisassembleFragment deviceInstallFragment;
    private DeviceDisassembleFragment deviceUninstallFragment;
    private ArrayList<BaseFragment> fragmentsList;
    private Device device;
    private String type;
    public static final String TYPE_DEVICE_INSTALL= "1";
    public static final String TYPE_DEVICE_UNINSTALL = "2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_disassemble);
        type = getIntent().getStringExtra("type");
        device = (Device) getIntent().getSerializableExtra("device");
        ButterKnife.bind(this);
        fragmentsList = new ArrayList<BaseFragment>();
        if (device != null) {
            saveTv.setVisibility(View.GONE);
            inOutTop.setVisibility(View.GONE);
            if (DeviceDisassembleListActivity.INSTALL.equals(type)) {
                deviceInstallFragment = DeviceDisassembleFragment.newInstance(TYPE_DEVICE_INSTALL);
                deviceInstallFragment.setData(device);
                fragmentsList.add(deviceInstallFragment);
//                inOutTop.check(R.id.in_rb);
                titleTv.setText("设备安装详情");
                titleTv.setVisibility(View.VISIBLE);
            } else if (DeviceDisassembleListActivity.UNINSTALL.equals(type)) {
                deviceUninstallFragment = DeviceDisassembleFragment.newInstance(TYPE_DEVICE_UNINSTALL);
                deviceUninstallFragment.setData(device);
                fragmentsList.add(deviceUninstallFragment);
                inOutTop.check(R.id.out_rb);
                titleTv.setText("设备拆卸详情");
                titleTv.setVisibility(View.VISIBLE);
            } else {
                deviceUninstallFragment = DeviceDisassembleFragment.newInstance(TYPE_DEVICE_UNINSTALL);
                deviceUninstallFragment.setData(device);
                fragmentsList.add(deviceUninstallFragment);
                inOutTop.check(R.id.out_rb);
                titleTv.setText("设备拆卸详情");
                titleTv.setVisibility(View.VISIBLE);
            }
            pagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragmentsList);
            deviceViewpager.setAdapter(pagerAdapter);
            Utils.disableRadioGroup(inOutTop);
        } else {
            deviceInstallFragment = DeviceDisassembleFragment.newInstance(TYPE_DEVICE_INSTALL);
            deviceUninstallFragment = DeviceDisassembleFragment.newInstance(TYPE_DEVICE_UNINSTALL);
            fragmentsList.add(deviceInstallFragment);
            fragmentsList.add(deviceUninstallFragment);
            initView();
            if (DeviceDisassembleListActivity.INSTALL.equals(type)) {
                deviceViewpager.setCurrentItem(0);
            } else if (DeviceDisassembleListActivity.UNINSTALL.equals(type)) {
                deviceViewpager.setCurrentItem(1);
            }
        }
    }

    private void initView() {
        pagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragmentsList);
        deviceViewpager.setAdapter(pagerAdapter);
        deviceViewpager.setOffscreenPageLimit(2);
        deviceViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    inOutTop.check(R.id.in_rb);
                } else {
                    inOutTop.check(R.id.out_rb);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        inRb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deviceViewpager.setCurrentItem(0, true);
            }
        });

        outRb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deviceViewpager.setCurrentItem(1, true);
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.save_tv})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.save_tv:
                if (deviceViewpager.getCurrentItem() == 0) {
                    fragmentsList.get(0).getMinImage("1");
                } else {
                    fragmentsList.get(1).getMinImage("2");
                }
                break;

        }
    }

}
